<template>
    <div class="main">
    <el-row type="flex" class="row-bg" justify="center">
        <el-col :span="8">
            <h2>掌上会议室系统</h2>
            <img src="../assets/icon/headerIcon.jpg" alt="" style="width: 200px; border-radius: 25%"/>
            <h4>Designed by:第十组</h4>
        </el-col>
        <el-col :span="1">
            <el-divider direction="vertical"></el-divider>
        </el-col>
        <el-col :span="8">
            <el-form :model="loginForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="账号" prop="name" style="width: 380px;">
                    <el-input v-model="loginForm.name"></el-input>
                </el-form-item>
                <el-form-item label="密码"  prop="password" style="width: 380px;">
                    <el-input v-model="loginForm.password"></el-input>
                </el-form-item>
                <el-form-item v-if="false" label="验证码" prop="code" style="width: 380px;">
                    <el-input v-model="loginForm.code" style="width: 150px;float: left"></el-input>
                    <el-image v-bind:src="captchaImg" class="captchaImg"></el-image>
                </el-form-item>
                <el-form-item label="身份" prop="identity" style="width: 380px" >
                    <el-radio-group v-model="loginForm.identity">
                        <el-radio label="用户"></el-radio>
                        <el-radio label="管理员"></el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </el-col>
    </el-row>
    </div>
</template>

<script>

export default {
  name: "login",
    data() {
        return {
            captchaImg:null,
            loginForm: {
                name: 'usergy',
                password:"gy",
                identity: '',
                code:"",
                token:"",

            },
            rules: {
                name: [
                    { required: true, message: '请输入账号', trigger: 'blur' }
                ],
                password: [
                    {required: true,message:"请输入密码",trigger:"blur"}
                ],
                code: [
                    {required: true, message:"请输入验证码",trigger:"blur"},
                    {mix: 5, max: 5, message: "请输入5个字符",trigger: "blur"}
                ],
                identity: [
                    { required: true, message: '请选择身份', trigger: 'change' }
                ],
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$axios({
                        method:'post',
                        url:this.loginForm.identity==="用户"?'http://localhost:80/user/userLogin':'http://localhost:80/admin/adminLogin',
                        params: {
                            name:this.loginForm.name,
                            password:this.loginForm.password
                        }
                    }).then(res =>{
                        console.log(res.data.data.uid)
                        // const jwt = res.headers['authorization']
                        //
                        // this.$store.commit('SET_TOKEN',jwt)
                        let id = res.data.data.uid
                        console.log(id)
                        window.sessionStorage.setItem('username', res.data.data.name)
                        window.sessionStorage.setItem("id",id )

                        console.log(window.sessionStorage.getItem("id"))

                        window.sessionStorage.setItem('userdata',res.data.data)
                        //this.$router.go(0)
                        this.loginForm.identity==="用户"?this.$router.push("/index"):this.$router.push("/AdminMainView")


                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        getCaptcha(){
            this.$axios({
                method:'get',
                url:'/captcha'
            }).then(res =>{
                console.log('/captcha')
                console.log(res)
                this.loginForm.token = res.data.data.token;
                this.captchaImg = res.data.data.captchaImg;
            })
        }
    },
    created() {
      this.getCaptcha()
    }
}
</script>

<style scoped>
    .main{
        background-image: url("../../src/assets/loginbkg.jpg");
        height: 100vh;
        width: 100%;
        justify-content:center;
        display: flex;
        align-items: center;
    }
    .el-row{
        border-radius: 50px;
        opacity: 0.8;
        display: flex;
        align-items: center;
        height: 450px;
        width: 750px;
        background: white;
        box-shadow: 5px 5px 5px black;

    }
    .el-col{
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-right: 20px;
    }
    .el-divider{
        height: 200px;
    }
    .captchaImg{
        float: left;
        margin-left: 8px;
        border-radius: 4px;
    }
</style>
